package com.tutucms.service.admin.controller;

import com.tutucms.common.model.response.Paging;
import com.tutucms.common.model.response.ResponseResult;
import com.tutucms.service.admin.model.ro.AddArticleRo;
import com.tutucms.service.admin.model.ro.ArticleRo;
import com.tutucms.service.admin.model.ro.EditArticleRo;
import com.tutucms.service.admin.service.ContentService;
import com.tutucms.service.auth.TokenValidator;
import com.tutucms.service.model.vo.content.ArticleFillVo;
import com.tutucms.service.model.vo.content.ArticleVo;
import com.tutucms.service.model.vo.content.CatalogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by wangxudong on 2020/10/26.
 *
 * @version: 1.0
 * @modified :
 */
@Slf4j
@Api(value = "内容管理系统", tags = "内容管理系统")
@TokenValidator
@RestController
@RequestMapping(value = "/content")
public class ContentController {

    @Autowired
    private ContentService contentService;

    @ApiOperation("根分类目录")
    @GetMapping("/root_catalog")
    public ResponseResult<List<CatalogVo>> getRootCatalog() {

        return ResponseResult.successResult(contentService.getRootCatalog());
    }

    @ApiOperation("分类目录")
    @GetMapping("/sub_catalog")
    public ResponseResult<List<CatalogVo>> getSubCatalog(@RequestParam("parentCode") String parentCode) {

        return ResponseResult.successResult(contentService.getSubCatalog(parentCode));
    }

    @ApiOperation("目录详情")
    @GetMapping("/catalog_detail")
    public ResponseResult<CatalogVo> getCatalogDetail(@RequestParam("code") String code) {

        return ResponseResult.successResult(contentService.getCatalogVo(code));
    }

    @ApiOperation("文章列表,根据catalogCode")
    @PostMapping("/article_list")
    public ResponseResult<Paging<ArticleVo>> getArticles(@Validated @RequestBody ArticleRo articleRo) {

        return ResponseResult.successResult(contentService.getArticleListByCode(articleRo));
    }

    @ApiOperation("文章详情")
    @GetMapping("/article_detail")
    public ResponseResult<ArticleFillVo> getCatalogDetail(@RequestParam("id") Long id) {

        return ResponseResult.successResult(contentService.getArticleVo(id));
    }

    @ApiOperation("删除文章")
    @GetMapping("/delete_article")
    public ResponseResult<Object> deleteArticle(@RequestParam("catalogCode") String catalogCode, @RequestParam("id") Long id) {

        contentService.deleteArticle(catalogCode, id);

        return ResponseResult.successResult(null, "删除成功");
    }

    @ApiOperation("添加文章")
    @PostMapping("/add_article")
    public ResponseResult<Object> addArticles(@Validated @RequestBody AddArticleRo addArticleRo) {

        contentService.addArticle(addArticleRo);
        return ResponseResult.successResult(null, "添加成功");
    }

    @ApiOperation("编辑文章")
    @PostMapping("/edit_article")
    public ResponseResult<Object> editArticles(@Validated @RequestBody EditArticleRo editArticleRo) {

        contentService.updateArticle(editArticleRo);
        return ResponseResult.successResult(null, "更新成功");
    }

    @ApiOperation(value = "调整顺序,action的值为up或down,id为文章id")
    @PostMapping("/sort_article")
    public ResponseResult<Object> sortArticle(@RequestParam("action") String action, @RequestParam("id") Long id) {
        contentService.sortArticle(id, action);

        return ResponseResult.successResult();
    }

    @ApiOperation("置顶/取消置顶")
    @PostMapping("/top_article")
    public ResponseResult<Object> topArticle(@RequestParam("id") Long id) {
        contentService.topArticle(id);

        return ResponseResult.successResult();
    }
}
